/**
 * Project Name:dt60_pure
 * File Name:MD5.java
 * Package Name:cn.java.utils
 * Date:涓婂崍11:59:07
 * Copyright (c) 2018, bluemobi All Rights Reserved.
 *
*/
package com.java.utils;

import java.security.MessageDigest;

/**
 * Description: MD5鍔犲瘑鐨勫伐鍏�<br/>
 * Date: 涓婂崍10:18:30 <br/>
 * 
 * @author 孙昱松
 * @version
 * @see
 */
public class MD5Tool {

	/**
	 * 瀹氫箟涓�16杩涘埗
	 */
	private static final String[] digital = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e",
			"f" };

	/**
	 * 
	 * Description: 閫氳繃MD5绠楁硶杩涜涓�娆″姞瀵�<br/>
	 *
	 * @author 涓侀箯
	 * @param txt锛氭槑鏂�
	 * @return锛氬姞瀵嗕箣鍚庣殑32浣嶇殑瀛楃涓�
	 * @throws Exception
	 */
	private static String encription(String txt) throws Exception {
		// 閫氳繃MD5瀵规槑鏂囪繘琛屽姞瀵�---->瀵嗘枃(32浣嶅瓧绗︿覆)
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		byte[] digest = md5.digest((txt + "").getBytes("UTF-8"));
		StringBuffer miWen = new StringBuffer("");
		for (byte b : digest) {
			int temp = b;
			if (temp < 0) {
				temp += 256;
			}
			// temp姝ゆ椂涓�瀹氫负姝ｆ暟
			int index1 = temp / 16;
			int index2 = temp % 16;
			// miWen += digital[index1] + digital[index2];
			miWen.append(digital[index1]).append(digital[index2]);
		}
		return miWen.toString();
	}

	/**
	 * 
	 * Description: md5鍔犲瘑鏈�缁堟柟娉�<br/>
	 *
	 * @author 涓侀箯
	 * @param txt
	 * @return
	 * @throws Exception
	 */
	public static String md5(String txt) throws Exception {
		return encription(encription(encription(txt) + txt + "dingPeng") + "dingPeng" + txt);
	}

}
